{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b213d19f",
   "metadata": {},
   "source": [
    "# 输出解析器"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef20db64",
   "metadata": {},
   "source": [
    "### 一、字符串输出解析器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7dab27bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.output_parsers import StrOutputParser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2180817b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.chat_models.tongyi import ChatTongyi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a20a3025",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8a9420e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = ChatPromptTemplate([\n",
    "    (\"system\", \"你是一个乐于助人的AI助手\"),\n",
    "    (\"user\", \"给我讲一个关于{topic}的笑话。\")\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c6a156ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ChatTongyi(\n",
    "    model=\"qwen-max\",\n",
    "    api_key=\"sk-da90821cf9174fbeb854011015c67aad\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9732e0c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = prompt | model | StrOutputParser()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e4f054d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当然可以，接下来是一个轻松的中秋节笑话：\n",
      "\n",
      "为什么月饼总是喜欢独自一人？\n",
      "因为它觉得自己很“饼”（棒）！ \n",
      "\n",
      "希望这个小笑话能为您的中秋佳节增添一份欢乐！\n"
     ]
    }
   ],
   "source": [
    "result = chain.invoke({\"topic\": \"中秋节\"})\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eea1817c",
   "metadata": {},
   "source": [
    "### 二、JSON输出解析器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2846d405",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.output_parsers import JsonOutputParser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "73777205",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f23d9e7d",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = ChatOpenAI(\n",
    "    base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "    api_key=\"sk-da90821cf9174fbeb854011015c67aad\",\n",
    "    model=\"qwen-max\"    \n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0d4efe46",
   "metadata": {},
   "outputs": [],
   "source": [
    "parser = JsonOutputParser()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d099e6be",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = model | parser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "73467584",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'question': 'LangChain的6大核心模块是什么？',\n",
       " 'answer': {'modules': [{'name': '模型(Model)'},\n",
       "   {'name': '提示(Prompt)'},\n",
       "   {'name': '索引(Index)'},\n",
       "   {'name': '链(Chain)'},\n",
       "   {'name': '内存(Memory)'},\n",
       "   {'name': '文档加载器(Document Loaders)'}]}}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.invoke(\"LangChain的6大核心模块是什么？用JSON格式回复，问题用question，回答用answer\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7cc2419c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "learn_langchain",
   "language": "python",
   "name": "learn_langchain"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
